const models = require('./devicedb').deviceList
const express = require('express')
const router = express.Router()
//const util = require("util")//可以使用 util.inspect 代替 JSON.stringify

/************** 创建(create) 读取(get) 更新(update) 删除(delete) **************/

//设备列表 
router.post('/device/getDevice', (req, res) => {
	models.find((err, docs) => {
		res.send(docs)
	})
});

//获取设备列表(分页)
router.post('/device/getDevicePage', (req, res) => {
	let Page = new models({
		pageSize: req.body.pageSize,
		skipPage: req.body.skipPage
	})
	let data;
	let totle;
	
	models.find((err, docs) => {
		// res.send(docs)
		totle = docs.length
	})
	models.find((err, docs) => {
		res.send({
			data: docs,
			total: totle
		})
	}).skip((Page.skipPage - 1) * Page.pageSize).limit(Page.pageSize)

});


// 创建设备
router.post('/device/createDevice', (req, res) => {
	// 这里的req.body能够使用就在index.js中引入了const bodyParser = require('body-parser')
	let newAccount = new models({
		name: req.body.name,
		createTime: req.body.createTime,
		updateTime: req.body.updateTime,
		status: req.body.status,
		desc: req.body.desc,
		content: req.body.content,
		deviceFrom: req.body.deviceFrom,
		buyer: req.body.buyer,
		deviceID: req.body.deviceID
	})
	//查询该设备ID是否已经被注册
	models.count({
		"deviceID": newAccount.deviceID
	}, (err, docs) => {
		if (err) {
			console.log("Error:" + err)
			res.send(err)
		} else {
			console.log("Docs:" + docs)
			if (docs >= 1) {
				//设备ID已经被注册，注册失败，返回code = 2
				console.log("新增失败")
				res.send({
					code: 2,
					msg: '设备ID已存在'
				})
			} else {
				// 设备ID未注册,保存数据newAccount数据进mongoDB，注册成功,返回code=1

				newAccount.save((err, data) => {
					//console.log('set')
					if (err) {
						res.send(err)
					} else {
						console.log("添加成功")
						res.send({
							code: 1,
							msg: '恭喜您，设备创建成功了'
						})
					}
				})
			}
		}
	})
});
//更新设备接口
router.post('/device/updateDevice', (req, res) => {
	// 这里的req.body能够使用就在index.js中引入了const bodyParser = require('body-parser')
	let newAccount = new models({
		name: req.body.name,
		createTime: req.body.createTime,
		updateTime: req.body.updateTime,
		status: req.body.status,
		desc: req.body.desc,
		content: req.body.content,
		deviceFrom: req.body.deviceFrom,
		buyer: req.body.buyer,
		deviceID: req.body.deviceID
	})

	models.findOne({"_id":req.body._id},(err,docs) => {
	 if(err){
	console.log("Error:" + err)
	res.send(err)
	 }else{
	  console.log("Docs:" + docs)
	  if(!docs == ""){
		  models.update({_id:req.body._id},{
			  $set: {
			  			name: req.body.name,
			  			updateTime: req.body.updateTime,
			  			status: req.body.status,
			  			desc: req.body.desc,
							content: req.body.content,
			  			deviceFrom: req.body.deviceFrom,
			  			buyer: req.body.buyer,
			  			deviceID: req.body.deviceID
			  		}
		  },(err) => {
		      if(err){
		        console.log(err);
		      }else{
		        console.log("更新成功");
		        res.send({code:1,msg:'更新成功'})
		      }
		    })
	  }else{
		  res.send({code:-2,msg:'更新失败，请了联系技术人员'})
	  }
	 }
	})
	
	
});



// 删除设备接口
router.post('/device/deleteDevice', (req, res) => {
	// 这里的req.body能够使用就在index.js中引入了const bodyParser = require('body-parser')
	console.log("id:"+ req.body._id)
	models.count({
		"_id": req.body._id
	}, (err, docs) => {
		if (err) {
			console.log("Error:" + err)
			res.send(err)
		} else {
			//console.log("Docs:" + docs)
			if (docs >= 1) {
				// 若注册，在mongoDB中删除该条数据，返回code = -1
				models.remove({
					"_id": req.body._id
				}, (err) => {
					//console.log('delete')
					if (err) return handleError(err);
				});
				console.log("删除成功")
				res.send({
					code: -1,
					msg: '设备已被删除'
				});
			} else {
				// 若还未注册，删除失败,返回code = -2，
				console.log("删除失败")
				res.send({
					code: -2,
					msg: '删除失败，请联系技术人员'
				});
			}
		}
	})
});

//删除多个
router.post('/device/deleteDeviceMore', (req, res) => {
	console.log(req.body)
	models.deleteMany({
		"_id":{$in:req.body},
		},(err) => {
		if(err){
			console.log("删除失败")
			res.send({
				code: -2,
				msg: '删除失败，请联系技术人员'
			});
			
		}else{
			console.log("删除成功")
			res.send({
				code: -1,
				msg: '设备已被删除'
			});
		}
	})
})
//根据名称查询
router.post('/device/findByName',(req,res) => {
	console.log(req.body.name)
	if(req.body.name){
		models.find({
			"name": {$regex :req.body.name},
		}, (err, docs) => {
			if(err){
				res.send({
					code: -2,
					msg: '查询失败，请联系技术人员'
				});
			}else{
				console.log('docs:'+docs)
				res.send(docs)
			}
		})
	}else{
		models.find((err, docs) => {
			res.send(docs)
		})
	}
		
	
});

module.exports = router
